package com.enbatis.mybatisplugs.service.impl;

import com.enbatis.mybatisplugs.base.Account;
import com.enbatis.mybatisplugs.base.BaseServiceImpl;

import com.enbatis.mybatisplugs.commons.sql.Wrapper;
import com.enbatis.mybatisplugs.entity.SysUser;
import com.enbatis.mybatisplugs.entity.TPurchaseCart;
import com.enbatis.mybatisplugs.entity.vo.TPurchaseCartVO;
import com.enbatis.mybatisplugs.mapper.TPurchaseCartMapper;
import com.enbatis.mybatisplugs.plugin.Pages;
import com.enbatis.mybatisplugs.service.TPurchaseCartService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
* 描述：
* @author wangweidong
* @date 2019-07-05 09:13:33
*/

@Service
public class TPurchaseCartServiceImpl extends BaseServiceImpl<TPurchaseCartMapper,TPurchaseCart > implements TPurchaseCartService {


    @Override
    public boolean addToCart(TPurchaseCart tPurchaseCart) {
        List<TPurchaseCart> list=list(new Wrapper<>(new TPurchaseCart()).eq("uid",tPurchaseCart.getUid())
        .eq("company_id",tPurchaseCart.getCompanyId()).eq("status",0).eq("product_id",tPurchaseCart.getProductId()));
        if (CollectionUtils.isEmpty(list)){
            tPurchaseCart.setCtime(new Date());
            tPurchaseCart.setStatus(0);
            return insert(tPurchaseCart)!=null;
        }
        TPurchaseCart db=list.get(0);
        BigDecimal num=db.getNum().add(tPurchaseCart.getNum());
        TPurchaseCart cart=new TPurchaseCart();
        cart.setNum(num);
        return update(cart,new Wrapper<>(new TPurchaseCart()).eq("id",db.getId()) )>0;
    }


    @Override
    public Pages<TPurchaseCartVO> pageVO(Page<TPurchaseCartVO> page, TPurchaseCartVO vo, Account sysUser) {
        vo.setCompanyId(sysUser.getCompanyId());
        vo.setUid(sysUser.getId());
        vo.setStatus(0);
        Page returnPage= PageHelper.startPage(page.getPageNum(),page.getPageSize(),page.getOrderBy());
        baseMapper.selectVOList(vo);
        return new Pages<>(returnPage);
    }


    @Override
    public List<TPurchaseCartVO> listVOByIds(Long[] ids) {

     return    baseMapper.selectVOListByIds(ids);
    }



}
